package org.enwey.icarenew.Activity.Login;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.APPUtils.Validator;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.R;


/**
 * Created by enwey on 2014/11/23.
 */
public class RegisterPhoneNumber extends BaseActivity {
    private EditText mPhoneNum;
    private TextView mNext;
    private TextView mProtocol;
    private TextView mAgree;
    private boolean isAgree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_phone);
        setUp();
        findView();
        registerListener();
    }

    private void setUp() {
        showLeftBtn();
        setTitle("手机号码注册");
    }

    private void findView() {
        mPhoneNum = (EditText) this.findViewById(R.id.activity_register_phone_phone);
        mNext = (TextView) this.findViewById(R.id.activity_register_phone_btn);
        mProtocol = (TextView) this.findViewById(R.id.activity_register_phone_protocol);
        mAgree = (TextView) this.findViewById(R.id.activity_register_phone_checkbox);
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.activity_register_phone_btn:
                        doNext();
                        break;
                    case R.id.activity_register_phone_protocol:
                        readProtocol();
                        break;
                    default:
                        break;
                }
            }
        };
        mNext.setOnClickListener(register);
        mProtocol.setOnClickListener(register);
    }

    private void doNext() {
        String phoneNum = mPhoneNum.getText().toString().trim();
        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtil.show(RegisterPhoneNumber.this, "手机号码不能为空!");
            return;
        }

        if (!Validator.isPhone(phoneNum)) {
            ToastUtil.show(RegisterPhoneNumber.this, "请输入正确手机号码!");
            return;
        }

        if (!isAgree) {
            ToastUtil.show(RegisterPhoneNumber.this, "请阅读用户手册!");
            return;
        }

        Intent intent = new Intent(RegisterPhoneNumber.this, RegisterSetPassword.class);
        intent.putExtra("phone", phoneNum);
        startActivity(intent);

    }

    private void readProtocol() {
        Intent intent = new Intent();
        intent.setClass(RegisterPhoneNumber.this, UseProtocolActivity.class);
        startActivityForResult(intent,0);
    }

    private void isRead() {
        Drawable checkbox;
        Resources resources = getResources();
        if (isAgree) {
            checkbox =  resources.getDrawable(R.drawable.ic_checkbox_checked_night);
            checkbox.setBounds(0, 0, checkbox.getMinimumWidth(), checkbox.getIntrinsicHeight());
        }else {
            checkbox =  resources.getDrawable(R.drawable.ic_checkbox_normal_night);
            checkbox.setBounds(0, 0, checkbox.getMinimumWidth(), checkbox.getIntrinsicHeight());
        }
        mAgree.setCompoundDrawables(checkbox,null,null,null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==20) {
            isAgree = data.getBooleanExtra("agree", false);
            isRead();
        }
    }

}
